package com.zzq.my.jetpacks.adapter;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 描述：万能分割线
 * 作者：zhuangzeqin
 * 时间: 2019/7/30-10:38
 * 邮箱：zzq@eeepay.cn
 * 备注:    //添加自定义分割线：可自定义分割线高度和颜色
 * lv_result.addItemDecoration(new RecyclerViewDivider(mContext, LinearLayoutManager.VERTICAL, 10, ContextCompat.getColor(mContext, R.color.line)));
 */
public class RecyclerViewDivider extends RecyclerView.ItemDecoration {
    private Paint mPaint;
    private Drawable mDivider;
    private int mDividerHeight = 2;//分割线高度，默认为1px
    private int mOrientation;//列表的方向：LinearLayoutManager.VERTICAL或LinearLayoutManager.HORIZONTAL
    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

    private int mDividerMarginLeft = 0;//设置左边距
    private int mDividerMarginRight = 0;//设置右边距

    /**
     * 26      * 默认分割线：高度为2px，颜色为灰色
     * 27      *
     * 28      * @param context
     * 29      * @param orientation 列表方向
     * 30
     */
    public RecyclerViewDivider(Context context, int orientation) {
        if (orientation != LinearLayoutManager.VERTICAL && orientation != LinearLayoutManager.HORIZONTAL) {
            throw new IllegalArgumentException("请输入正确的参数！");
        }
        mOrientation = orientation;

        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
    }

    /**
     * 43      * 自定义分割线
     * 44      *
     * 45      * @param context
     * 46      * @param orientation 列表方向
     * 47      * @param drawableId  分割线图片
     * 48
     */
    public RecyclerViewDivider(Context context, int orientation, int drawableId) {
        this(context, orientation);
        mDivider = ContextCompat.getDrawable(context, drawableId);
        mDividerHeight = mDivider.getIntrinsicHeight();
    }

    /**
     * 56      * 自定义分割线
     * 57      *
     * 58      * @param context
     * 59      * @param orientation   列表方向
     * 60      * @param dividerHeight 分割线高度
     * 61      * @param dividerColor  分割线颜色
     * 62
     */
    public RecyclerViewDivider(Context context, int orientation, int dividerHeight, int dividerColor) {
        this(context, orientation);
        mDividerHeight = dividerHeight;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(dividerColor);
        mPaint.setStyle(Paint.Style.FILL);

    }

    /**
     * 56      * 自定义分割线
     * 57      *
     * 58      * @param context
     * 59      * @param orientation   列表方向
     * 60      * @param dividerHeight 分割线高度
     * 61      * @param dividerColor  分割线颜色
     * 62      * @param dividerMarginLeft  分割线距离左边值
     * 63      * @param dividerMarginRight 分割线距离右边值
     */
    public RecyclerViewDivider(Context context, int orientation, int dividerHeight, int dividerColor, int dividerMarginLeft, int dividerMarginRight) {
        this(context, orientation);
        mDividerHeight = dividerHeight;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(dividerColor);
        mPaint.setStyle(Paint.Style.FILL);
        mDividerMarginLeft = dividerMarginLeft;//设置左边距
        mDividerMarginRight = dividerMarginRight;//设置右边距
    }

    //获取分割线尺寸
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.set(0, 0, 0, mDividerHeight);
    }

    //绘制分割线
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (mOrientation == LinearLayoutManager.VERTICAL) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    //绘制横向 item 分割线
    private void drawHorizontal(Canvas canvas, RecyclerView parent) {
        int left = 0;
        if (mDividerMarginLeft > 0) {
            left = mDividerMarginLeft;
        } else {
            left = parent.getPaddingLeft();
        }
        int right = 0;
        if (mDividerMarginRight > 0) {
            right = parent.getMeasuredWidth() - mDividerMarginRight;
        } else {
            right = parent.getMeasuredWidth() - parent.getPaddingRight();
        }
        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + layoutParams.bottomMargin;
            final int bottom = top + mDividerHeight;
            if (mDivider != null) {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(canvas);
            }
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }

    //绘制纵向 item 分割线
    private void drawVertical(Canvas canvas, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + layoutParams.rightMargin;
            final int right = left + mDividerHeight;
            if (mDivider != null) {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(canvas);
            }
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }
}
